package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.entity.SetmealDish;
import com.sky.mapper.DishMapper;
import com.sky.mapper.FlavorsMappre;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@Service
public class DishServiceImpl implements DishService {
    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private FlavorsMappre flavorsMappre;

    @Autowired
    private SetmealMapper setmealMapper;

    @Autowired
    private SetmealDishMapper setmealDishMapper;

    /**
     * 新增菜品
     * @param dto
     */

    @Transactional
    @Override
    public void insertDish(DishDTO dto) {
        Dish dish=new Dish();
        BeanUtils.copyProperties(dto,dish);
        dishMapper.insert(dish);

        log.info("菜品id："+dish.getId());

        //菜品的口味
        List<DishFlavor> flavors = dto.getFlavors();
        flavors.forEach(flavor->{
            flavor.setDishId(dish.getId());
        });

        //新增菜品的口味
        flavorsMappre.insert(flavors);
    }

    /**
     * 分页查询
     * @param pageQueryDTO
     * @return
     */
    @Override
    public PageResult pageQuery(DishPageQueryDTO pageQueryDTO) {
        //分页参数
        PageHelper.startPage(pageQueryDTO.getPage(),pageQueryDTO.getPageSize());


        List<DishVO> dishVOS = dishMapper.pageQuery(pageQueryDTO);
        Page page= (Page) dishVOS;


//        dishVOS.forEach(dishVO -> {
//            log.info("=======================================");
//            log.info("跟新最新时间跟踪："+dishVO.getUpdateTime());
//            log.info("=======================================");
//
//        });


        return new PageResult(page.getTotal(),page.getResult());
    }

    /**
     * 状态修改
     * @param status
     * @param id
     */
    @Override
    public void updateStatus(Integer status, Long id) {
        Dish dish =new Dish();
        dish.setStatus(status);
        dish.setId(id);
        dishMapper.updateStatus(dish);
    }


//    @Override
//    public List<Dish> getDishById(Integer id) {
//        return  dishMapper.getDishById(id);
//    }


    @Override
    public Result getDishById(Long id) {
//        //根据id查菜品
//        DishVO dishById = dishMapper.getDishById(id);

        //根据id查菜品
        DishVO dishById = dishMapper.getDishById(id);

//        //根据id查菜品关联的额flavors
//        List<DishFlavor> dishFlavors = flavorsMappre.getflavorsById(id);
//
//        //将菜品口味加入菜品
//        dishById.setFlavors(dishFlavors);



        return Result.success(dishById);
    }


    @Override
    public List<Dish> getDishListByCategoryId(Long categoryId) {
//        dishMapper.getDishListByCategoryId( categoryId);
        return dishMapper.getDishListByCategoryId( categoryId);
    }


    /**
     * 修改菜品
     * @param dishVO
     */

    @Transactional
    @Override
    public void updateDish(DishVO dishVO) {
        Dish dish =new Dish();
        BeanUtils.copyProperties(dishVO,dish);

        //分开修改 先修改菜品信息
        dishMapper.updateDish(dish);

        log.info("公共字段填充后："+dish);

        //修改口味信息
        List<DishFlavor> flavors = dishVO.getFlavors();

        //将菜品的口味信息删出完了
        if(flavors==null || flavors.size()<=0){
            //根据菜品id删除对应的菜品口味
            flavorsMappre.deleteByListId(dishVO.getId());
            return;
        }

        //新添加的菜品口味没有菜品id
        flavors.forEach(flavor->{
            flavor.setDishId(dish.getId());
        });

        //当修改了口味时需要将之前的口味信息全部删除再添加
        //根据菜品id删除对应的菜品口味
        flavorsMappre.deleteByListId(dishVO.getId());



        //删除完后再添加口味信息
        flavorsMappre.insert(flavors);


    }


    /**
     * 删除菜品
     * @param ids
     * @return
     */
    @Transactional
    @Override
    public Result deleteById(List<Long> ids) {
        //起售的菜品不能删除！
        List<Dish> dishByStatus = dishMapper.getDishByStatus(ids);
        if(dishByStatus.size()>0){
            log.info("起售中不能删除");
            return Result.error(MessageConstant.DISH_ON_SALE);
        }


        //TODO 无套餐信息可能需要优化
        //被套餐关联的菜品不能删除
        List<SetmealDish> setmealDishbyDishId = setmealDishMapper.getByDishId(ids);
        if(setmealDishbyDishId.size()>0){
            log.info("被套餐关联的菜品不能删除");
            return Result.error(MessageConstant.DISH_BE_RELATED_BY_SETMEAL);
        }

        //可批量删除菜品！
        dishMapper.deleteById(ids);
        //批量删除口味
        flavorsMappre.deleteByListIds(ids);

        return Result.success(MessageConstant.DISH_DLETE_SUCCEES);
    }









    /**
     * 条件查询菜品和口味
     * @param dish
     * @return
     */
    public List<DishVO> listWithFlavor(Dish dish) {
        List<Dish> dishList = dishMapper.list(dish);

        List<DishVO> dishVOList = new ArrayList<>();

        for (Dish d : dishList) {
            DishVO dishVO = new DishVO();
            BeanUtils.copyProperties(d,dishVO);

            //根据菜品id查询对应的口味
            List<DishFlavor> flavors = flavorsMappre.getByDishId(d.getId());

            dishVO.setFlavors(flavors);
            dishVOList.add(dishVO);
        }

        return dishVOList;
    }
}
